import Vue from "vue";
import * as dayjs from "dayjs";

Vue.filter('NumberFormat', function (value) {
  if (!value) {
    return '0'
  }
  let intPartFormat = value.toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1,') //将整数部分逢三一断
  return intPartFormat
})

Vue.filter('dayjs', function (dataStr, pattern = 'YYYY-MM-DD HH:mm:ss') {
  return dayjs(dataStr).format(pattern)
})

Vue.filter('moment', function (dataStr, pattern = 'YYYY-MM-DD HH:mm:ss') {
  return dayjs(dataStr).format(pattern)
})

/** 字符串超长截取省略号显示 */
Vue.filter('ellipsis', function (value, vlength = 25) {
  if (!value) {
    return "";
  }
  console.log('vlength: ' + vlength);
  if (value.length > vlength) {
    return value.slice(0, vlength) + '...'
  }
  return value
})

Vue.filter('utc2beijing', function (utcDatetime) {
  if (utcDatetime) {
    const utcDatetimeArr = utcDatetime.split('+')
    const utcDate = utcDatetimeArr[0] + 'Z'
    // 转为正常的时间格式 年-月-日 时:分:秒
    var tPos = utcDate.indexOf('T')
    var zPos = utcDate.indexOf('Z')
    var yearMonthDay = utcDate.substr(0, tPos)
    var hourMinuteSecond = utcDate.substr(tPos + 1, zPos - tPos - 1)
    var newDatetime = yearMonthDay + ' ' + hourMinuteSecond // 2017-03-31 08:02:06
    // 增加8个小时，北京时间比utc时间多八个时区
    var timeStamp
    // 处理成为时间戳
    timeStamp = new Date(Date.parse(newDatetime))
    timeStamp = timeStamp.getTime()
    timeStamp = timeStamp / 1000
    // 增加8个小时，北京时间比utc时间多八个时区
    timeStamp = timeStamp + 8 * 60 * 60
    // 时间戳转为时间
    var time = new Date(parseInt(timeStamp) * 1000) // 时间戳为10位需*1000,13位则无需
    var Y = time.getFullYear() + '-'
    var M = (time.getMonth() + 1 < 10 ? '0' + (time.getMonth() + 1) : time.getMonth() + 1) + '-'
    var D = (time.getDate() < 10 ? '0' + time.getDate() : time.getDate()) + ' '
    var h = (time.getHours() < 10 ? '0' + time.getHours() : time.getHours()) + ':'
    var m = (time.getMinutes() < 10 ? '0' + time.getMinutes() : time.getMinutes()) + ':'
    var s = time.getSeconds() < 10 ? '0' + time.getSeconds() : time.getSeconds()
    return Y + M + D + h + m + s
  }
})

Vue.filter('timeStamp', function (timeStamp) {
  if (timeStamp) {
    var time = new Date(parseInt(timeStamp) * 1000) // 时间戳为10位需*1000,13位则无需
    var Y = time.getFullYear() + '-'
    var M = (time.getMonth() + 1 < 10 ? '0' + (time.getMonth() + 1) : time.getMonth() + 1) + '-'
    var D = (time.getDate() < 10 ? '0' + time.getDate() : time.getDate()) + ' '
    // var h = (time.getHours() < 10 ? '0' + time.getHours() : time.getHours()) + ':'
    // var m = (time.getMinutes() < 10 ? '0' + time.getMinutes() : time.getMinutes()) + ':'
    // var s = time.getSeconds() < 10 ? '0' + time.getSeconds() : time.getSeconds()
    // return Y + M + D + h + m + s
    return Y + M + D
  }
})

Vue.filter('gender', function (v) {
  if (v == 0) {
    return '女'
  } else if (v == 1) {
    return '男'
  }
})